//
//  OrderModel.m
//  DaJiang
//
//  Created by sally on 16/10/25.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "ExpertInterface.h"
#import "OrderInterface.h"
#import "OrderModel.h"

@implementation OrderModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"orderid" }];
}

//我约的行家
+ (void)queryMyOrderListWithCursor:(NSInteger)cursor
                              type:(OrderStatusType)type
                        completion:(void (^)(NSMutableArray *orderList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          OrderModel *orderModel = [[OrderModel alloc] initWithDictionary:item error:nil];
          [self setOrderStateDetailDesc:orderModel];
          [retMArray addObject:orderModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface queryMyOrderListWithCursor:cursor type:type completion:_completion error:_error];
}

//约我的人
+ (void)queryExpertOrderListWithCursor:(NSInteger)cursor
                                  type:(OrderStatusType)type
                            completion:(void (^)(NSMutableArray *orderList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          OrderModel *orderModel = [[OrderModel alloc] initWithDictionary:item error:nil];
          [self setOrderStateDetailDesc:orderModel];
          [retMArray addObject:orderModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryExpertOrderListWithCursor:cursor type:type completion:_completion error:_error];
}

#pragma mark - 约见接口相关
#pragma mark - 普通用户端
//预约专家的一个话题
+ (void)bookOrderWithProTopicId:(NSInteger)proTopicId
                   proTopicName:(NSString *)proTopicName
                  proTopicPrice:(CGFloat)proTopicPrice
                         proUid:(NSInteger)proUid
                        problem:(NSString *)problem
                          intro:(NSString *)intro
                         proPic:(NSString *)proPic
                        proName:(NSString *)proName
                       proTitle:(NSString *)proTitle
                     completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface bookOrderWithProTopicId:proTopicId proTopicName:proTopicName proTopicPrice:proTopicPrice proUid:proUid problem:problem intro:intro proPic:proPic proName:proName proTitle:proTitle completion:_completion error:_error];
}

//确认付费
+ (void)surePayedWithConsultId:(NSInteger)consultId
                        proUid:(NSInteger)proUid
                     cashValue:(CGFloat)cashValue
                     topicName:(NSString *)topicName
                    completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface surePayedWithConsultId:consultId proUid:proUid cashValue:cashValue topicName:topicName completion:_completion error:_error];
}

//用户选择专家有空时间
+ (void)chooseFreeWithFreeId:(NSInteger)freeId
                   consultId:(NSInteger)consultId
                    meetTime:(NSString *)meetTime
                     meetLoc:(NSString *)meetLoc
                  completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  AccountInterface *interface = [[AccountInterface alloc] init];
  [interface chooseFreeWithFreeId:freeId consultId:consultId meetTime:meetTime meetLoc:meetLoc completion:_completion error:_error];
}

//用户取消约见
+ (void)userCancelOrderWithReason:(NSString *)reason
                        consultId:(NSInteger)consultId
                       completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface userCancelOrderWithReason:reason consultId:consultId completion:_completion error:_error];
}

//提醒专家
+ (void)remindExpertWithProUid:(NSInteger)proUid
                     consultId:(NSInteger)consultId
                    completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface remindExpertWithProUid:proUid consultId:consultId completion:_completion error:_error];
}

//评价专家
+ (void)commentExpertWithComment:(NSString *)comment
                       topicName:(NSString *)topicName
                          proUid:(NSInteger)proUid
                        cmtValue:(NSInteger)cmtValue
                         topicId:(NSInteger)topicId
                       consultId:(NSInteger)consultId
                      completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface commentExpertWithComment:comment topicName:topicName proUid:proUid cmtValue:cmtValue topicId:topicId consultId:consultId completion:_completion error:_error];
}

#pragma mark - 专家端
//专家取消约见
+ (void)expertCancelOrderWithConsultId:(NSInteger)consultId
                            completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface expertCancelOrderWithConsultId:consultId completion:_completion error:_error];
}

//专家接受约见
+ (void)expertConfirmOrderWithConsultId:(NSInteger)consultId
                             completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface expertConfirmOrderWithConsultId:consultId completion:_completion error:_error];
}

//用户失约
+ (void)userMissedMeetWithConsultId:(NSInteger)consultId
                         completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface userMissedMeetWithConsultId:consultId completion:_completion error:_error];
}

//专家确认已经见面
+ (void)hasBeenMeetWithConsultId:(NSInteger)consultId
                      completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface hasBeenMeetWithConsultId:consultId completion:_completion error:_error];
}

//装配订单状态描述
+ (void)setOrderStateDetailDesc:(OrderModel *)orderModel {
  NSString *stateDetail = orderModel.op_data.state_detail;
  //TODO: maybe文案要替换
  if ([stateDetail isEqualToString:@"book"]) { //用户预订
    orderModel.op_data.state_detail_desc = @"待审核";
  } else if ([stateDetail isEqualToString:@"admin_pass"]) { //后台审核通过*
    orderModel.op_data.state_detail_desc = @"待确认";
  } else if ([stateDetail isEqualToString:@"admin_reject"]) { //后台审核拒绝
    orderModel.op_data.state_detail_desc = @"已拒绝";
  } else if ([stateDetail isEqualToString:@"confirmed"]) { //专家确认可以见面
    orderModel.op_data.state_detail_desc = @"待付款";
  } else if ([stateDetail isEqualToString:@"user_payed"]) { //用户已支付*
    orderModel.op_data.state_detail_desc = @"待见面";
  } else if ([stateDetail isEqualToString:@"has_been_met"]) { //专家已确认见面*
    orderModel.op_data.state_detail_desc = @"已见面";
  } else if ([stateDetail isEqualToString:@"commented"]) { //用户已评价*
    orderModel.op_data.state_detail_desc = @"已评价";
  } else if ([stateDetail isEqualToString:@"cancelled"]) { //用户取消见面
    orderModel.op_data.state_detail_desc = @"已取消";
  } else if ([stateDetail isEqualToString:@"user_missed_meet"]) { //用户没有来（失约）*
    orderModel.op_data.state_detail_desc = @"用户失约";
  } else if ([stateDetail isEqualToString:@"pro_user_refused"]) { //专家取消
    orderModel.op_data.state_detail_desc = @"已拒绝";
  }
}

@end

@implementation OrderData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end
